/*
 * @Author: 0x9DEFA478
 * @Date: 2025-07-09 20:21:51
 * @LastEditTime: 2025-09-13 17:17:29
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * 
 */
#include "./H_Tree_RedBlack.h"




static inline int ReplaceChildNode(H_TreeNode* Parent,H_TreeNode* Node,H_TreeNode* ChildNode){

  if(Parent->Left==Node){// Left
    Parent->Left=ChildNode;
    return 1;
  }
  // Right
  Parent->Right=ChildNode;
  
  return 0;
}

static int RemoveNode_LR(H_TreeNode_RedBlack** Tree,H_TreeNode** Node){
  H_TreeNode* tNode;
  H_TreeNode* lNode;
  H_TreeNode* node;

  tNode=*Node;
  lNode=tNode->Left;

  node=lNode;
  while(node->Right!=NULL){
    node=node->Right;
  }

  if(node==lNode){//左子节点就已经没有右节点的情况
    H_TreeNode* Parent;
    H_TreeNode* LL;
    H_TreeNode* Right;
    int lNode_IsRed;

    Parent=tNode->Parent;
    LL=lNode->Left;
    Right=tNode->Right;
    lNode_IsRed=H_TreeNode_To_H_TreeNode_RedBlack(lNode)->IsRed;
    if((H_TreeNode_To_H_TreeNode_RedBlack(tNode)->IsRed!=0)||(lNode_IsRed!=0)){

      /**
       * 
       *         tNode:B                             lNode:B
       *          /   \                               /  \ 
       *         /     \           ------>           /    \ 
       *        /       \                           /      \
       *     lNode:R  Right:X1                    LL:B    Right:X1
       *       / 
       *      / 
       *   LL:B
       * 
       */
      
      /**
       *     
       * 
       *         tNode:R                             lNode:R
       *          /   \                               /   \ 
       *         /     \           ------>           /     \ 
       *        /       \                           /       \ 
       *     lNode:B  Right:B                     LL:B     Right:B
       *      /        /   \                                /   \ 
       *     /        /     \                              /     \ 
       *   LL:R
       */

      /**
       *     
       * 
       *         tNode:R                             lNode:R
       *          /   \                               /   \ 
       *         /     \           ------>           x     \ 
       *        /       \                           /       \ 
       *     lNode:B  Right:B                    tNode:B    Right:B
       *               /   \                                /   \ 
       *              /     \                              /     \ 
       * 
       */

      lNode->Parent=Parent;
      Right->Parent=lNode;

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(lNode);
      }else{
        ReplaceChildNode(Parent,tNode,lNode);
      }
      lNode->Right=Right;

      if(lNode_IsRed!=0){
        H_TreeNode_To_H_TreeNode_RedBlack(lNode)->IsRed=0;
      }else{
        H_TreeNode_To_H_TreeNode_RedBlack(lNode)->IsRed=-1;
        if(LL!=NULL){
          H_TreeNode_To_H_TreeNode_RedBlack(LL)->IsRed=0;
        }else{
          *Node=lNode;
          return 1;
        }
      }
    }else{
      /**
       *         LL!=NULL
       * 
       *         tNode:B                             lNode:B
       *          /   \                               /  \ 
       *         /     \           ------>           /    \ 
       *        /       \                           /      \
       *     lNode:B  Right:X1                    LL:B  Right:X1
       *       / 
       *      / 
       *    LL:R
       * 
       */

      /**
       *         LL==NULL
       * 
       *         tNode:B                             lNode:B
       *          /   \                               /  \ 
       *         /     \           ------>           x    \ 
       *        /       \                           /      \
       *     lNode:B  Right:X1                  tNode:B  Right:X1
       * 
       */

      lNode->Parent=Parent;
      Right->Parent=lNode;

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(lNode);
      }else{
        ReplaceChildNode(Parent,tNode,lNode);
      }
      lNode->Right=Right;
      if(LL!=NULL){
        H_TreeNode_To_H_TreeNode_RedBlack(LL)->IsRed=0;
      }else{
        *Node=lNode;
        return 1;
      }
    }
  }else{
    H_TreeNode* Parent;
    H_TreeNode* ChildNode;
    H_TreeNode* resNode;

    resNode=NULL;

    //移除前驱node
    {
      Parent=node->Parent;
      ChildNode=node->Left;
      if(ChildNode!=NULL){
        ChildNode->Parent=Parent;
        H_TreeNode_To_H_TreeNode_RedBlack(ChildNode)->IsRed=0;
      }else{
        if(H_TreeNode_To_H_TreeNode_RedBlack(node)->IsRed==0){
          //移除黑色导致失衡
          resNode=Parent;
        }
      }
      Parent->Right=ChildNode;
      // *Node=Parent;//结构上被去除节点的父节点
    }

    //前驱节点node移动 到 逻辑上被去除节点 的位置
    *H_TreeNode_To_H_TreeNode_RedBlack(node)=*H_TreeNode_To_H_TreeNode_RedBlack(tNode);//节点信息复制
    Parent=tNode->Parent;
    if(Parent==NULL){
      *Tree=H_TreeNode_To_H_TreeNode_RedBlack(node);
    }else{
      ReplaceChildNode(Parent,tNode,node);
    }
    ChildNode=node->Left;
    ChildNode->Parent=node;
    ChildNode=node->Right;
    ChildNode->Parent=node;

    if(resNode!=NULL){
      *Node=resNode;
      return 0;
    }
  }
  
  *Node=NULL;//无需继续递归
  return 0;
}


static inline H_TreeNode* adjustNode_L(H_TreeNode_RedBlack** Tree,H_TreeNode* Node){
  H_TreeNode* Parent;
  H_TreeNode* L;
  H_TreeNode* LL;
  H_TreeNode* LR;

  Parent=Node->Parent;

  L=Node->Left;//此时L肯定存在
  LR=L->Right;
  if(H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed!=0){

    /**
     *     这种情况, 将Node变红可避免部分向上递归, 情况变成后续的情况之一
     * 
     *            Node:B                             L:B
     *            /   \                             /   \ 
     *           /     x                           /     \ 
     *          /       \           ------>       /       \ 
     *        L:R      [R:B]                    LL:B     Node:R
     *       /   \                                        /  \ 
     *      /     \                                      /    x 
     *    LL:B   LR:B                                  LR:B  [R:B]
     * 
     */

    Node->Parent=L;
    L->Parent=Parent;
    LR->Parent=Node;
    
    if(Parent==NULL){
      *Tree=H_TreeNode_To_H_TreeNode_RedBlack(L);
    }else{
      ReplaceChildNode(Parent,Node,L);
    }
    Node->Left=LR;
    L->Right=Node;

    H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=-1;
    H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed=0;

    //新的状态
    Parent=L;
    L=LR;
    LR=L->Right;
  }

  //L肯定为黑色

  LL=L->Left;
  if(LL==NULL){
    if(LR!=NULL){
      /**
       * 
       *            Node:X                            LR:X
       *            /   \                             /   \ 
       *           /     x                           /     \ 
       *          /       \           ------>       /       \ 
       *        L:B      [R:B]                    L:B      Node:B
       *           \                                          \ 
       *            \                                          x 
       *           LR:R                                       [R:B]
       * 
       */

      Node->Parent=LR;
      L->Parent=LR;
      LR->Parent=Parent;

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(LR);
      }else{
        ReplaceChildNode(Parent,Node,LR);
      }
      Node->Left=NULL;
      L->Right=NULL;
      LR->Left=L;
      LR->Right=Node;

      H_TreeNode_To_H_TreeNode_RedBlack(LR)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
      H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

      return NULL;
    }
  }else{
    if(H_TreeNode_To_H_TreeNode_RedBlack(LL)->IsRed!=0){
      /**
       * 
       *            Node:X0                            L:X0
       *            /   \                             /   \ 
       *           /     x                           /     \ 
       *          /       \           ------>       /       \ 
       *        L:B      [R:B]                    LL:B     Node:B
       *       /   \                                        /  \ 
       *      /     \                                      /    x 
       *    LL:R   LR:X1                                 LR:X1 [R:B]
       * 
       */

      Node->Parent=L;
      L->Parent=Parent;
      if(LR!=NULL){
        LR->Parent=Node;
      }

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(L);
      }else{
        ReplaceChildNode(Parent,Node,L);
      }
      Node->Left=LR;
      L->Right=Node;

      H_TreeNode_To_H_TreeNode_RedBlack(LL)->IsRed=0;
      H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
      H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

      return NULL;
    }else{

      //因为LL为黑色 LR肯定不为NULL

      if(H_TreeNode_To_H_TreeNode_RedBlack(LR)->IsRed!=0){
        H_TreeNode* LRL;
        H_TreeNode* LRR;
        /**
         * 
         *            Node:X                             LR:X
         *            /   \                             /   \ 
         *           /     x                           /     \ 
         *          /       \           ------>       /       \ 
         *        L:B      [R:B]                     L:B     Node:B
         *       /   \                              /  \      /  \ 
         *      /     \                            /    \    /    x 
         *    LL:B   LR:R                      LL:B  LRL:B LRR:B  [R:B]
         *           /  \
         *       LRL:B  LRR:B
         * 
         */

        LRL=LR->Left;
        LRR=LR->Right;

        Node->Parent=LR;
        L->Parent=LR;
        LR->Parent=Parent;
        LRL->Parent=L;
        LRR->Parent=Node;

        if(Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(LR);
        }else{
          ReplaceChildNode(Parent,Node,LR);
        }
        Node->Left=LRR;
        L->Right=LRL;
        LR->Left=L;
        LR->Right=Node;

        H_TreeNode_To_H_TreeNode_RedBlack(LR)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
        H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

        return NULL;
      }
    }
  }

  H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed=-1;
  if(H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed!=0){
    /**
     *  LL==NULL
     * 
     *            Node:R                            Node:B
     *            /   \                             /   \ 
     *           /     x                           /     x 
     *          /       \           ------>       /       \ 
     *        L:B      [R:B]                    L:R     [R:B]
     * 
     */

    /**
     *  LL!=NULL
     * 
     *            Node:R                            Node:B
     *            /   \                             /   \ 
     *           /     x                           /     x
     *          /       \           ------>       /       \ 
     *        L:B      [R:B]                    L:R      [R:B]
     *       /   \                             /   \ 
     *      /     \                           /     \ 
     *    LL:B   LR:B                       LL:B   LR:B
     * 
     */

    H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;
    // H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed=-1;//在该if的前方设置
  }else{
    /**
     *  LL==NULL  需要向上递归
     * 
     *            Node:B                            Node:B
     *            /   \                             /   \ 
     *           /     x                           /     x 
     *          /       \           ------>       /       \ 
     *        L:B      [R:B]                    L:R     [R:B]
     * 
     */

    /**
     *  LL!=NULL  需要向上递归
     * 
     *            Node:B                            Node:B
     *            /   \                             /   \ 
     *           /     x                           /     x
     *          /       \           ------>       /       \ 
     *        L:B      [R:B]                    L:R      [R:B]
     *       /   \                             /   \ 
     *      /     \                           /     \ 
     *    LL:B   LR:B                       LL:B   LR:B
     * 
     */
    
    // H_TreeNode_To_H_TreeNode_RedBlack(L)->IsRed=-1;//在该if的前方设置
    
    return Node;
  }
  
  return NULL;
}

static inline H_TreeNode* adjustNode_R(H_TreeNode_RedBlack** Tree,H_TreeNode* Node){
  H_TreeNode* Parent;
  H_TreeNode* R;
  H_TreeNode* RR;
  H_TreeNode* RL;

  Parent=Node->Parent;

  R=Node->Right;//此时L肯定存在
  RL=R->Left;
  if(H_TreeNode_To_H_TreeNode_RedBlack(R)->IsRed!=0){

    Node->Parent=R;
    R->Parent=Parent;
    RL->Parent=Node;
    
    if(Parent==NULL){
      *Tree=H_TreeNode_To_H_TreeNode_RedBlack(R);
    }else{
      ReplaceChildNode(Parent,Node,R);
    }
    Node->Right=RL;
    R->Left=Node;

    H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=-1;
    H_TreeNode_To_H_TreeNode_RedBlack(R)->IsRed=0;

    //新的状态
    Parent=R;
    R=RL;
    RL=R->Left;
  }

  //R肯定为黑色

  RR=R->Right;
  if(RR==NULL){
    if(RL!=NULL){

      Node->Parent=RL;
      R->Parent=RL;
      RL->Parent=Parent;

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(RL);
      }else{
        ReplaceChildNode(Parent,Node,RL);
      }
      Node->Right=NULL;
      R->Left=NULL;
      RL->Right=R;
      RL->Left=Node;

      H_TreeNode_To_H_TreeNode_RedBlack(RL)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
      H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

      return NULL;
    }
  }else{
    if(H_TreeNode_To_H_TreeNode_RedBlack(RR)->IsRed!=0){

      Node->Parent=R;
      R->Parent=Parent;
      if(RL!=NULL){
        RL->Parent=Node;
      }

      if(Parent==NULL){
        *Tree=H_TreeNode_To_H_TreeNode_RedBlack(R);
      }else{
        ReplaceChildNode(Parent,Node,R);
      }
      Node->Right=RL;
      R->Left=Node;

      H_TreeNode_To_H_TreeNode_RedBlack(RR)->IsRed=0;
      H_TreeNode_To_H_TreeNode_RedBlack(R)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
      H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

      return NULL;
    }else{

      //因为RR为黑色 RL肯定不为NULL

      if(H_TreeNode_To_H_TreeNode_RedBlack(RL)->IsRed!=0){
        H_TreeNode* RLL;
        H_TreeNode* RLR;

        RLL=RL->Left;
        RLR=RL->Right;

        Node->Parent=RL;
        R->Parent=RL;
        RL->Parent=Parent;
        RLL->Parent=Node;
        RLR->Parent=R;

        if(Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(RL);
        }else{
          ReplaceChildNode(Parent,Node,RL);
        }
        Node->Right=RLL;
        R->Left=RLR;
        RL->Left=Node;
        RL->Right=R;

        H_TreeNode_To_H_TreeNode_RedBlack(RL)->IsRed=H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed;
        H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

        return NULL;
      }
    }
  }

  H_TreeNode_To_H_TreeNode_RedBlack(R)->IsRed=-1;
  if(H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed!=0){
    H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;
  }else{
    return Node;
  }
  
  return NULL;
}


static void treeFix_Insert(H_TreeNode_RedBlack** Tree,H_TreeNode* Node,H_TreeNode* SNode){

  //被添加的子节点肯定是红色 本节点肯定是红色 父节点肯定是黑色 树的黑色节点是平衡的 但是需要解决颜色冲突

  for(;;){
    H_TreeNode* Parent;
    H_TreeNode* Parent_Parent;

    Parent=Node->Parent;//Parent肯定不为NULL
    Parent_Parent=Parent->Parent;
    if(Parent->Left==Node){
      {
        H_TreeNode* ParentR;
        ParentR=Parent->Right;

        if((ParentR!=NULL)&&(H_TreeNode_To_H_TreeNode_RedBlack(ParentR)->IsRed!=0)){
          /**
           *      父节点黑色下放, 需要向上检查颜色冲突
           * 
           *        Parent:B                              Parent:R
           *         /   \                                 /   \ 
           *        /     \                               /     \ 
           *       /       \          ------>            /       \ 
           *    Node:R  ParentR:R                     Node:B  ParentR:B
           *      |                                     |
           *   SNode:R                               SNode:R
           */

          H_TreeNode_To_H_TreeNode_RedBlack(ParentR)->IsRed=0;
          H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

          if(Parent_Parent==NULL){
            break;//根节点无需变色 处理结束
          }
          H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
          if(H_TreeNode_To_H_TreeNode_RedBlack(Parent_Parent)->IsRed==0){
            break;//颜色仍未冲突
          }

          SNode=Parent;
          Node=Parent_Parent;
          continue;
        }
      }

      //ParentR此时不存在或者为黑色

      if(Node->Left==SNode){
        H_TreeNode* R;

        /**
         *    LL
         * 
         *        Parent:B                               Node:B
         *         /   \                                 /   \ 
         *        /     \                               /     \ 
         *       /       \          ------>            /       \ 
         *     Node:R  ParentR:B                    SNode:R   Parent:R
         *     /   \                                 /  \     /   \
         *    /     \                               /    \   /     \
         * SNode:R  R:B                                     R:B   ParentR:B
         *  /  \
         */

        R=Node->Right;

        Parent->Parent=Node;
        Node->Parent=Parent_Parent;
        if(R!=NULL){
          R->Parent=Parent;
        }

        if(Parent_Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(Node);
        }else{
          ReplaceChildNode(Parent_Parent,Parent,Node);
        }
        Parent->Left=R;
        Node->Right=Parent;

        H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
        H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;
        // H_TreeNode_To_H_TreeNode_RedBlack(SNode)->IsRed=-1;//颜色未变化 无需修改
      }else{
        H_TreeNode* RL;
        H_TreeNode* RR;

        /**
         *    LR
         * 
         *         Parent:B                             SNode:B
         *          /   \                                /   \ 
         *         /     \                              /     \ 
         *        /       \          ------>           /       \ 
         *     Node:R   ParentR:B                   Node:R    Parent:R
         *     /   \                                /   \     /   \ 
         *    /     \                              /     \   /     \ 
         *        SNode:R                              RL:B  RR:B ParentR:B
         *         /   \
         *       RL:B  RR:B
         */
        
        RL=SNode->Left;
        RR=SNode->Right;

        Parent->Parent=SNode;
        Node->Parent=SNode;
        SNode->Parent=Parent_Parent;
        if(RL!=NULL){
          RL->Parent=Node;
        }
        if(RR!=NULL){
          RR->Parent=Parent;
        }

        if(Parent_Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(SNode);
        }else{
          ReplaceChildNode(Parent_Parent,Parent,SNode);
        }
        Parent->Left=RR;
        Node->Right=RL;
        SNode->Left=Node;
        SNode->Right=Parent;

        H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
        // H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=-1;//颜色未变化 无需修改
        H_TreeNode_To_H_TreeNode_RedBlack(SNode)->IsRed=0;
      }
    }else{
      {
        H_TreeNode* ParentL;
        ParentL=Parent->Left;

        if((ParentL!=NULL)&&(H_TreeNode_To_H_TreeNode_RedBlack(ParentL)->IsRed!=0)){

          H_TreeNode_To_H_TreeNode_RedBlack(ParentL)->IsRed=0;
          H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;

          if(Parent_Parent==NULL){
            break;//根节点无需变色 处理结束
          }
          H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
          if(H_TreeNode_To_H_TreeNode_RedBlack(Parent_Parent)->IsRed==0){
            break;//颜色仍未冲突
          }

          SNode=Parent;
          Node=Parent_Parent;
          continue;
        }
      }

      if(Node->Right==SNode){
        H_TreeNode* L;

        L=Node->Left;

        Parent->Parent=Node;
        Node->Parent=Parent_Parent;
        if(L!=NULL){
          L->Parent=Parent;
        }

        if(Parent_Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(Node);
        }else{
          ReplaceChildNode(Parent_Parent,Parent,Node);
        }
        Parent->Right=L;
        Node->Left=Parent;

        H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
        H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=0;
        // H_TreeNode_To_H_TreeNode_RedBlack(SNode)->IsRed=-1;//颜色未变化 无需修改
      }else{
        H_TreeNode* LL;
        H_TreeNode* LR;
        
        LL=SNode->Left;
        LR=SNode->Right;

        Parent->Parent=SNode;
        Node->Parent=SNode;
        SNode->Parent=Parent_Parent;
        if(LR!=NULL){
          LR->Parent=Node;
        }
        if(LL!=NULL){
          LL->Parent=Parent;
        }

        if(Parent_Parent==NULL){
          *Tree=H_TreeNode_To_H_TreeNode_RedBlack(SNode);
        }else{
          ReplaceChildNode(Parent_Parent,Parent,SNode);
        }
        Parent->Right=LL;
        Node->Left=LR;
        SNode->Right=Node;
        SNode->Left=Parent;

        H_TreeNode_To_H_TreeNode_RedBlack(Parent)->IsRed=-1;
        // H_TreeNode_To_H_TreeNode_RedBlack(Node)->IsRed=-1;//颜色未变化 无需修改
        H_TreeNode_To_H_TreeNode_RedBlack(SNode)->IsRed=0;
      }
    }
    break;
  }
}

static void treeFix_Remove(H_TreeNode_RedBlack** Tree,H_TreeNode* Node,int IsL){

  for(;;){
    H_TreeNode* NewNode;
    if(IsL==0){
      NewNode=adjustNode_L(Tree,Node);
    }else{
      NewNode=adjustNode_R(Tree,Node);
    }

    if(NewNode==NULL){
      break;
    }
    Node=NewNode->Parent;
    if(Node==NULL){
      break;
    }
    if(Node->Left==NewNode){
      IsL=1;
    }else{
      IsL=0;
    }
  }

}




void H_Tree_RedBlack_InsertTo(H_TreeNode_RedBlack** Tree,H_TreeNode_RedBlack* Node,H_TreeNode_RedBlack* ReturnNode,int FindInsert_Result){
  
  Node->Node.Left=NULL;
  Node->Node.Right=NULL;

  switch(FindInsert_Result){
    case vH_Tree_FindInsert_Result_InsertToLeft:
      Node->IsRed=-1;
      ReturnNode->Node.Left=&Node->Node;
      Node->Node.Parent=&ReturnNode->Node;
      if(ReturnNode->IsRed!=0){
        //父节点为红 需要处理冲突
        treeFix_Insert(Tree,&ReturnNode->Node,&Node->Node);
      }
      break;
    case vH_Tree_FindInsert_Result_InsertToRight:
      Node->IsRed=-1;
      ReturnNode->Node.Right=&Node->Node;
      Node->Node.Parent=&ReturnNode->Node;
      if(ReturnNode->IsRed!=0){
        //父节点为红 需要处理冲突
        treeFix_Insert(Tree,&ReturnNode->Node,&Node->Node);
      }
      break;
    case vH_Tree_FindInsert_Result_InsertToRoot:
      Node->IsRed=0;
      *Tree=Node;
      Node->Node.Parent=NULL;
      break;
    default:
      break;
  }
  
}

void H_Tree_RedBlack_Remove(H_TreeNode_RedBlack** Tree,H_TreeNode_RedBlack* Node){
  H_TreeNode* CurrentNode;
  H_TreeNode* Left;
  H_TreeNode* Right;

  CurrentNode=&Node->Node;
  Left=CurrentNode->Left;
  Right=CurrentNode->Right;
  if(Left!=NULL){
    if(Right!=NULL){// L:Left R:Right
      int IsL;

      IsL=RemoveNode_LR(Tree,&CurrentNode);
      if(CurrentNode!=NULL){
        treeFix_Remove(Tree,CurrentNode,IsL);//平衡节点
      }
    }else{// L:Left R:NULL
      H_TreeNode* Parent;
      H_TreeNode_RedBlack* left;
      Parent=CurrentNode->Parent;
      Left->Parent=Parent;
      left=H_TreeNode_To_H_TreeNode_RedBlack(Left);
      if(Parent==NULL){
        *Tree=left;
      }else{
        ReplaceChildNode(Parent,CurrentNode,Left);
      }
      left->IsRed=0;
    }
  }else{
    if(Right!=NULL){// L:NULL R:Right
      H_TreeNode* Parent;
      H_TreeNode_RedBlack* right;
      Parent=CurrentNode->Parent;
      Right->Parent=Parent;
      right=H_TreeNode_To_H_TreeNode_RedBlack(Right);
      if(Parent==NULL){
        *Tree=right;
      }else{
        ReplaceChildNode(Parent,CurrentNode,Right);
      }
      right->IsRed=0;
    }else{// L:NULL R:NULL
      H_TreeNode* Parent;
      Parent=CurrentNode->Parent;
      if(Parent==NULL){
        *Tree=NULL;
      }else{
        int IsL;

        IsL=ReplaceChildNode(Parent,CurrentNode,NULL);
        if(H_TreeNode_To_H_TreeNode_RedBlack(CurrentNode)->IsRed==0){
          treeFix_Remove(Tree,Parent,IsL);
        }
      }
    }
  }
}



